package com.dsiab.manageService.security.config;

import com.dsiab.manageService.jwt.JwtTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig  {

    @Autowired
    JwtTokenFilter jwtTokenFilter;
    @Autowired
    SecurityLogoutSuccessHandler securityLogoutSuccessHandler;

    @Bean
    protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 禁用session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
        .and()
        .authorizeHttpRequests()
        // 放行登录请求
        .requestMatchers("/account/login").permitAll()
        // 放行退出请求
        .requestMatchers("/account/logout").permitAll()
        // 放行注册请求
        .requestMatchers("/account/register").permitAll()
        // 放行所有get请求
        .requestMatchers("/get*").permitAll()
        .requestMatchers("/*/get*").permitAll()
        .requestMatchers("/*/get*/*").permitAll()
        // 放行不需要校验用户信息接口
        .requestMatchers("/tools/*").permitAll()// 放行工具接口
        .requestMatchers("/util/*").permitAll()
        .requestMatchers("/stats/*").permitAll()
        // 放行swagger3相关资源
        // 访问地址：http://localhost:8091/bootService/swagger-ui/index.html
        .requestMatchers("/swagger-ui/*").permitAll()
        .requestMatchers("/webjars/*").permitAll()
        .requestMatchers("/v3/*").permitAll()
        .requestMatchers("/swagger-resources/*").permitAll()
        // 放行静态文件
        .requestMatchers("/*/*.html").permitAll()
        // 其他请求都要求认证
        .anyRequest().authenticated()
        .and()
        // 将退出接口映射到controller接口
        .logout().logoutUrl("/account/logout").logoutSuccessHandler(securityLogoutSuccessHandler)
        .and()
        // 允许跨域
        .csrf().disable();
        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }


}
